# Maintainer: Maxime Gauduin <alucryd@archlinux.org>
# Contributor: Guillaume Alaux <guillaume@archlinux.org>

pkgname=tomcat7
pkgver=7.0.109
pkgrel=1
pkgdesc='Open source implementation of the Java Servlet 3.0 and JavaServer Pages 2.2 technologies'
arch=(any)
url=https://tomcat.apache.org/
license=(APACHE)
depends=(
  'java-runtime>=6'
  java-jsvc
  java-commons-daemon
  eclipse-ecj
)
makedepends=(
  ant
  git
  'java-environment=8'
)
optdepends=('tomcat-native: to allow optimal performance in production environments')
backup=(
  etc/tomcat7/catalina.policy
  etc/tomcat7/catalina.properties
  etc/tomcat7/context.xml
  etc/tomcat7/logging.properties
  etc/tomcat7/server.xml
  etc/tomcat7/tomcat-users.xml
  etc/tomcat7/web.xml
)
install=tomcat7.install
_tag=2cdef2c0241cdf70b5edd88d3733a52e6b675047
source=(
  git+https://github.com/apache/tomcat.git#tag=${_tag}
  tomcat7.service
  tomcat7.sysusers
  tomcat7.tmpfiles
)
b2sums=('SKIP'
        'e8931e1b6ef796617eaad93c3a32e996fe9887ae07ac0459fd5a13342704289c4ff77149d12f273254de72567f0334b77b4cd435e47c795567184ac21579d44d'
        'a84b82434c1ca47fca30fd67a4af07260ee0b1f16450420317750587dd2ca54cc3e3c20e3475ffcee9e4302cce54fdd53f2d27cab25c80b42256f6816437c3a2'
        'c014d435c9903e0b5d66c45ffcc2c3bbb46a087958e9281042031fd6a41595b9be6fc07fc9be388911d5b4832fa99c4d13b768ba526adbef9087b28d13347189')

_gid_log=19
_gid_tomcat=71
_uid_tomcat=71

pkgver() {
  cd tomcat

  _pkgver=$(git describe --tags)

  if [[ ${_pkgver} != 7.* ]]; then
    exit 1
  fi

  echo ${_pkgver}
}

prepare() {
  cd tomcat

  cp build.properties.default build.properties
  sed "/^base\.path=/c\base\.path=${srcdir}" -i build.properties
  sed "/^compile.debug=/c\compile.debug=false" -i build.properties
  sed "/^trydownload.httpusecaches=/c\trydownload.httpusecaches=false" -i build.properties
  sed "/^#java.7.home=/c\java.7.home=/usr/lib/jvm/java-8-openjdk" -i build.properties
}

build() {
  cd tomcat

  export JAVA_HOME=/usr/lib/jvm/java-8-openjdk

  ant
}

package() {
  cd tomcat/output/build

  # Tomcat general files
  install -dm 755 "${pkgdir}"/usr/share/tomcat7/bin
  install -m 755  bin/* -t "${pkgdir}"/usr/share/tomcat7/bin/
  # commons-daemon is packaged on its own
  rm "${pkgdir}"/usr/share/tomcat7/bin/{*.bat,commons-daemon*}
  ln -s /usr/share/java/commons-daemon.jar "${pkgdir}"/usr/share/tomcat7/bin/commons-daemon.jar

  install -dm 755 "${pkgdir}"/usr/share/java/tomcat7
  install -m 644 lib/* -t "${pkgdir}"/usr/share/java/tomcat7/
  # eclipse-ecj is packaged on its own
  rm "${pkgdir}"/usr/share/java/tomcat7/ecj-*.jar
  ln -s ../eclipse-ecj.jar "${pkgdir}"/usr/share/java/tomcat7/ecj.jar

  ln -s /usr/share/java/tomcat7 "${pkgdir}"/usr/share/tomcat7/lib

  # We log through systemd but this would still be required for stock Tomcat logging
  install -dm 775 -o ${_uid_tomcat} -g ${_gid_log} "${pkgdir}"/var/log/tomcat7
  ln -s /var/log/tomcat7 "${pkgdir}"/usr/share/tomcat7/logs
  touch "${pkgdir}"/var/log/tomcat7/catalina.{out,err}
  chgrp ${_gid_log} "${pkgdir}"/var/log/tomcat7/catalina.{out,err}

  install -dm 775 "${pkgdir}"/etc/tomcat7
  install -m 640 -g ${_gid_tomcat} conf/* -t "${pkgdir}"/etc/tomcat7/

  install -dm 775 -g ${_gid_tomcat} "${pkgdir}"/etc/tomcat7/Catalina
  ln -s /etc/tomcat7 "${pkgdir}"/usr/share/tomcat7/conf

  install -dm 775 "${pkgdir}"/var/lib/tomcat7
  cp -dr --no-preserve=ownership webapps "${pkgdir}"/var/lib/tomcat7/
  chown -R ${_uid_tomcat}:${_gid_tomcat} "${pkgdir}"/var/lib/tomcat7
  chmod 775 "${pkgdir}"/var/lib/tomcat7/webapps
  ln -s /var/lib/tomcat7/webapps "${pkgdir}"/usr/share/tomcat7/webapps

  install -dm 1777 "${pkgdir}"/var/tmp
  install -dm 775 -o ${_uid_tomcat} -g ${_gid_tomcat} "${pkgdir}"/var/tmp/tomcat7/{temp,work}
  ln -s /var/tmp/tomcat7/temp "${pkgdir}"/usr/share/tomcat7/temp
  ln -s /var/tmp/tomcat7/work "${pkgdir}"/usr/share/tomcat7/work

  install -Dm 644 ../../../tomcat7.service "${pkgdir}"/usr/lib/systemd/system/tomcat7.service
  install -Dm 644 ../../../tomcat7.sysusers "${pkgdir}"/usr/lib/sysusers.d/tomcat7.conf
  install -Dm 644 ../../../tomcat7.tmpfiles "${pkgdir}"/usr/lib/tmpfiles.d/tomcat7.conf
}

# vim: ts=2 sw=2 et:
